<template>
    <div class="Main">

        <div v-for="(item,i) in fileList" :key="i" class="item">
            <el-card class="box-card">
                <div slot="header" class="clearfix">
                    <div class="title">
                        <span class="mainTile" @click="goToDetailArticle(item.id)" v-text=item.title></span>
                        <span class="tags">
                              <el-tag
                                      style="margin: 10px"
                                      v-for="tag in item.tags"
                                      :key="tag"

                              >
                                 {{tag}}
                              </el-tag>

                        </span>
                    </div>

                    <span class="head"></span><span class="time">{{toData(item.updateTime)}}</span>

                </div>
                <div class="item-content" v-text="handleBreviary(item.content)">
                </div>
                <div class="item-img" :style="{backgroundImage:'url('+ item.img+')'}">

                </div>
            </el-card>
        </div>

        <el-pagination
                background
                @current-change="showArticle"
                :current-page="currentPage"
                :page-size="10"
                layout="total, prev, pager, next, jumper"
                :total="totalNums">
        </el-pagination>


    </div>
</template>

<script>
    export default {
        name: "HomePage",
        data() {
            return {
                fileList: [
                    {
                        author: "",
                        content: "",
                        id: "",
                        img: "",
                        title: "",
                        updateTime: "",

                    }],
                DetailArticle: null,
                totalNums: 0,
                currentPage: 1,
                pageNums: 0

            }
        },
        components: {},
        mounted() {
            this.showArticle(1)

        },
        methods: {
            showArticle(val) {
                let _this = this
                this.currentPage = val;
                this.$API.getArticle(
                    {
                        currentPage: _this.currentPage,
                        pageSize: 10
                    }
                ).then(
                    res => {
                        this.fileList = res.data["data"]

                    }
                )
                this.$API.getArticleTotalNums().then(
                    res => {
                        this.totalNums = Number(res.data["data"])
                        this.pageNums = Math.ceil(Number(res.data["data"]) / 10)

                    }
                )


            },

            backHome() {

                this.DetailArticle = null
            },
            editArticle() {
                this.$store.dispatch('changeDetailArticle', this.DetailArticle)
                this.$router.push('/write')


            },
            goToDetailArticle(id) {    //转到详情文章的页面
                this.$router.push("/Article")
                this.$store.dispatch("changeArticleId",id)


            },

            toData(timestamp) {
                let now = Number(timestamp)
                var date = new Date(now);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
                var Y = date.getFullYear() + '-';
                var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
                var D = date.getDate() + ' ';
                var h = date.getHours() + ':';
                var m = date.getMinutes()
                return Y + M + D + h + m;

            }, handleBreviary(str) {
                let con = str.replace(/\s*/g, "")
                    .replace(/<[^>]+>/g, "")
                    .replace(/↵/g, "")
                    .replace(/[\r\n]/g, "")
                    .replace(/&nbsp;/ig, "")
                    .slice(0, 100) + "......";
                return con;

            },


        }

    }
</script>

<style lang="scss" scoped>
    .Main {

        .item {

            margin: 5px;
            width: 70%;
            display: inline-block;

            .box-card {


                .clearfix {
                    background: white;
                    cursor: pointer;

                    .head {

                        float: left;
                        border-radius: 50%;
                        width: 30px;
                        height: 30px;
                        background-image: url("../../assets/images/portrait.jpg");
                        background-size: cover;
                        display: inline-block;
                    }

                    .time {
                        float: left;
                        line-height: 30px;
                        height: 30px;
                        font-size: 13px;
                        margin-left: 5px;
                        color: gray;
                    }

                    .title {
                        height: 30px;
                        width: 100%;

                        .mainTile {
                            float: left;
                            /*width: ;*/
                            font-weight: bold;
                            text-align: left;
                            /*font-weight: 500;*/
                            font-size: 18px;
                            line-height: 28px;
                            color: #212121;
                            position: relative;
                            vertical-align: middle;
                        }

                        .tags {
                            float: right;

                        }
                    }


                }

                .item-content {
                    /*height: 70px;*/
                    text-align: initial;
                    margin: 10px 10px 2vw 0;
                    float: left;
                    width: 80%;
                    margin-bottom: 15px;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    display: -webkit-box;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: horizontal;
                    line-height: 1.5;
                    color: #888;
                    font-size: 15px;
                    font-family: -apple-system, BlinkMacSystemFont, Helvetica Neue, PingFang SC, Microsoft YaHei, Source Han Sans SC, Noto Sans CJK SC, WenQuanYi Micro Hei, sans-serif;
                }


                .item-img {
                    margin-bottom: 10px;
                    height: 50px;
                    width: 50px;
                    border-radius: 5px;
                    float: left;
                    background-size: cover;
                    float: right;


                }
            }


        }

        .detailArticle {
            text-align: -webkit-auto;
        }

        .mainContent {

            border-color: #888888;
            background: white;
            /*border:  solid grey;*/

        }


    }


</style>
